module.exports = app => {
    const { STRING, INTEGER, DATE, BOOLEAN } = app.Sequelize;

    const User = app.model.define(
        'user',
        {
            username: {
                type: STRING(32),
                allowNull: false
            },
            password: {
                type: STRING(32),
                allowNull: false
            },
            type: {
                type: INTEGER,
                allowNull: false,
                defaultValue: 1
            },
            state: {
                type: INTEGER,
                allowNull: false,
                defaultValue: 1
            },
            grant_type: {
                type: INTEGER,
                allowNull: false,
                defaultValue: 1
            },
            role_id: {
                type: INTEGER,
                allowNull: true
            },
            third_id: {
                type: STRING(64),
                allowNull: true
            },
            gender: {
                type: INTEGER,
                allowNull: true
            },
            birthday: {
                type: DATE,
                allowNull: true
            },
            nickname: {
                type: STRING(32),
                allowNull: true
            },
            phone: {
                type: STRING(32),
                allowNull: true
            },
            email: {
                type: STRING(32),
                allowNull: true
            },
            avatar: {
                type: STRING(255),
                allowNull: true
            },
            note: {
                type: STRING(255),
                allowNull: true
            },
        },
        {
            tableName: 'tbl_user',
            timestamps: true,
            underscored: true,
            charset: 'utf8',
            collate: 'utf8_general_ci'
        });

    User.createTable = async () => {
        try {
            return await User.sync({force: true});
        } catch (error) {
            return error.message;
        }
    };

    User.findByLogin = async (name) => {
        return await this.findOne({
            where: {
                name
            }
        });
    };

    User.prototype.logSignin = async () => {
        await this.update({ last_sign_in_at: new Date() });
    };

    return User;
};